All files / src/components/device DeviceManagementInterface.tsx

0% Statements 0/28
0% Branches 0/45
0% Functions 0/4
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
'use client';
 
import React, { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Smartphone, Users, BarChart3, AlertTriangle } from 'lucide-react';
import { deviceService } from '@/services/device';
import ActiveDeviceList from './ActiveDeviceList';
import DeviceRemovalInterface from './DeviceRemovalInterface';
import DeviceLimitWarning from './DeviceLimitWarning';
import { useTranslation } from 'react-i18next';
import useLoadNamespace from '@/hooks/useLoadNamespace';
 
 
interface DeviceManagementInterfaceProps {
  className?: string;
}
 
export default function DeviceManagementInterface({ className }: DeviceManagementInterfaceProps) {
  const [activeTab, setActiveTab] = useState('overview');
  // Load admin/devices namespace and use it exclusively to avoid raw dotted keys
  useLoadNamespace('admin/devices');
  const { t } = useTranslation('admin/devices');
 
  // Fetch device statistics
  const { data: deviceStats, isLoading: isStatsLoading, error: statsError } = useQuery({
    queryKey: ['device-stats'],
    queryFn: async () => {
      const result = await deviceService.getDeviceStats();
      if (result.success) {
        return result.data;
      }
      throw new Error(result.error.details);
    }});
 
  // Fetch all devices for admin view
  const { data: allDevicesData, isLoading: isDevicesLoading, error: devicesError } = useQuery({
    queryKey: ['all-devices'],
    queryFn: async () => {
      const result = await deviceService.getAllDevices();
      if (result.success) {
        return result.data;
      }
      throw new Error(result.error.details);
    }});
 
  if (statsError || devicesError) {
    return (
      <Card className={className}>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <Smartphone className="h-5 w-5" />
            {t('devices.title')}
          </CardTitle>
        </CardHeader>
        <CardContent>
          <Alert variant="destructive">
            <AlertTriangle className="h-4 w-4" />
            <AlertDescription>
              {t('devices.errors.loadFailed')}
            </AlertDescription>
          </Alert>
        </CardContent>
      </Card>
    );
  }
 
  return (
    <div className={className}>
      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <Smartphone className="h-5 w-5" />
            {t('devices.title')}
          </CardTitle>
          <CardDescription>
            {t('devices.description')}
          </CardDescription>
        </CardHeader>
        <CardContent>
          <Tabs value={activeTab} onValueChange={setActiveTab} className="space-y-6">
            <TabsList className="grid w-full grid-cols-4">
              <TabsTrigger value="overview" className="flex items-center gap-2">
                <BarChart3 className="h-4 w-4" />
                {t('devices.overviewTitle')}
              </TabsTrigger>
              <TabsTrigger value="active-devices" className="flex items-center gap-2">
                <Smartphone className="h-4 w-4" />
                {t('devices.activeDevices')}
              </TabsTrigger>
              <TabsTrigger value="device-removal" className="flex items-center gap-2">
                <AlertTriangle className="h-4 w-4" />
                {t('devices.deviceRemoval')}
              </TabsTrigger>
              <TabsTrigger value="warnings" className="flex items-center gap-2">
                <AlertTriangle className="h-4 w-4" />
                {t('devices.warnings')}
              </TabsTrigger>
            </TabsList>
 
            {/* Overview Tab */}
            <TabsContent value="overview" className="space-y-6">
              <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-4">
                {/* Total Devices */}
                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('devices.totalDevices')}</CardTitle>
                    <Smartphone className="h-4 w-4 text-muted-foreground" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">
                      {isStatsLoading ? '...' : deviceStats?.total_devices || 0}
                    </div>
                    <p className="text-xs text-muted-foreground">
                      {t('devices.registeredDevices')}
                    </p>
                  </CardContent>
                </Card>
 
                {/* Active Devices */}
                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('devices.activeDevices')}</CardTitle>
                    <Smartphone className="h-4 w-4 text-green-600" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">
                      {isStatsLoading ? '...' : deviceStats?.active_devices || 0}
                    </div>
                    <p className="text-xs text-muted-foreground">
                      {t('devices.currentlyConnected')}
                    </p>
                  </CardContent>
                </Card>
 
                {/* Users with Devices */}
                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('devices.usersWithDevices')}</CardTitle>
                    <Users className="h-4 w-4 text-muted-foreground" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">
                      {isStatsLoading ? '...' : deviceStats?.users_with_devices || 0}
                    </div>
                    <p className="text-xs text-muted-foreground">
                      {t('devices.activeUsers')}
                    </p>
                  </CardContent>
                </Card>
 
                {/* Device Limit Warnings */}
                <Card>
                  <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                    <CardTitle className="text-sm font-medium">{t('devices.limitWarnings')}</CardTitle>
                    <AlertTriangle className="h-4 w-4 text-yellow-600" />
                  </CardHeader>
                  <CardContent>
                    <div className="text-2xl font-bold">
                      {isStatsLoading ? '...' : deviceStats?.users_at_limit || 0}
                    </div>
                    <p className="text-xs text-muted-foreground">
                      {t('devices.usersAtLimit')}
                    </p>
                  </CardContent>
                </Card>
              </div>
 
              {/* Device Statistics Section */}
              <Card>
                <CardHeader>
                  <CardTitle className="flex items-center gap-2">
                    <BarChart3 className="h-5 w-5" />
                    {t('devices.deviceStatistics.title')}
                  </CardTitle>
                  <CardDescription>
                    {t('devices.statsDescription')}
                  </CardDescription>
                </CardHeader>
                <CardContent className="space-y-6">
                  {/* Summary Metrics */}
                  <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
                    <div className="p-4 border rounded-lg">
                      <div className="flex items-center gap-2 mb-2">
                        <BarChart3 className="h-4 w-4 text-blue-600" />
                        <span className="text-sm font-medium">{t('devices.avgDevicesPerUser')}</span>
                      </div>
                      <div className="text-2xl font-bold">
                        {isStatsLoading ? '...' : deviceStats?.avg_devices_per_user?.toFixed(1) || '0'}
                      </div>
                    </div>
 
                    <div className="p-4 border rounded-lg">
                      <div className="flex items-center gap-2 mb-2">
                        <AlertTriangle className="h-4 w-4 text-red-600" />
                        <span className="text-sm font-medium">{t('devices.usersAtLimit')}</span>
                      </div>
                      <div className="text-2xl font-bold">
                        {isStatsLoading ? '...' : deviceStats?.users_at_limit || 0}
                      </div>
                    </div>
 
                    <div className="p-4 border rounded-lg">
                      <div className="flex items-center gap-2 mb-2">
                        <Smartphone className="h-4 w-4 text-green-600" />
                        <span className="text-sm font-medium">{t('devices.totalCapacity')}</span>
                      </div>
                      <div className="text-2xl font-bold">
                        {isStatsLoading ? '...' : deviceStats?.total_capacity || 0}
                      </div>
                    </div>
 
                    <div className="p-4 border rounded-lg">
                      <div className="flex items-center gap-2 mb-2">
                        <BarChart3 className="h-4 w-4 text-purple-600" />
                        <span className="text-sm font-medium">{t('devices.utilization')}</span>
                      </div>
                      <div className="text-2xl font-bold">
                        {isStatsLoading ? '...' : `${deviceStats?.utilization || 0}%`}
                      </div>
                    </div>
                  </div>
 
                  {/* Device Usage by User */}
                  <div>
                    <h3 className="text-lg font-medium mb-4">{t('devices.usageByUserTitle')}</h3>
                    {!deviceStats?.devices_by_user || deviceStats.devices_by_user.length === 0 ? (
                      <div className="text-center py-8">
                        <Users className="h-12 w-12 text-gray-400 mx-auto mb-4" />
                        <h3 className="text-lg font-medium text-gray-900 mb-2">
                          {t('devices.deviceUsageByUser.none')}
                        </h3>
                        <p className="text-gray-500">
                          {t('devices.usageByUserDescription')}
                        </p>
                      </div>
                    ) : (
                      <div className="space-y-2">
                        {deviceStats.devices_by_user.map((user) => (
                          <div key={user.user_id} className="flex items-center justify-between p-3 border rounded-lg">
                            <div className="flex items-center gap-2">
                              <Users className="h-4 w-4 text-muted-foreground" />
                              <span className="font-medium">{user.username}</span>
                            </div>
                            <div className="flex items-center gap-4">
                              <span className="text-sm text-muted-foreground">
                                {t('devices.overview.deviceCount', { count: user.device_count, max: user.max_devices })}
                              </span>
                              <div className="w-24 bg-gray-200 rounded-full h-2">
                                <div
                                  className="bg-blue-600 h-2 rounded-full"
                                  style={{ width: `${Math.min((user.device_count / user.max_devices) * 100, 100)}%` }}
                                ></div>
                              </div>
                            </div>
                          </div>
                        ))}
                      </div>
                    )}
                  </div>
 
                  {/* Usage Distribution */}
                  <div>
                    <h3 className="text-lg font-medium mb-4">{t('devices.usageDistribution')}</h3>
                    <div className="grid gap-4 md:grid-cols-2">
                      <div>
                        <h4 className="text-sm font-medium mb-2">{t('devices.byUsageLevel')}</h4>
                        <div className="space-y-2">
                          <div className="flex items-center justify-between">
                            <div className="flex items-center gap-2">
                              <div className="w-3 h-3 bg-green-500 rounded-full"></div>
                              <span className="text-sm">{t('devices.lowUsage')}</span>
                            </div>
                            <span className="text-sm font-medium">0</span>
                          </div>
                        </div>
                      </div>
                      <div>
                        <h4 className="text-sm font-medium mb-2">{t('devices.systemHealth.title')}</h4>
                        <div className="space-y-2">
                          <div className="flex items-center justify-between">
                            <span className="text-sm">{t('devices.activeDevices')}</span>
                            <span className="text-sm font-medium">{deviceStats?.active_devices || 0}</span>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </CardContent>
              </Card>
            </TabsContent>
 
            {/* Active Devices Tab */}
            <TabsContent value="active-devices">
                <ActiveDeviceList 
                  devicesData={allDevicesData}
                  isLoading={isDevicesLoading}
                />
            </TabsContent>
 
            {/* Device Removal Tab */}
            <TabsContent value="device-removal">
                <DeviceRemovalInterface 
                  devicesData={allDevicesData}
                  isLoading={isDevicesLoading}
                />
            </TabsContent>
 
            {/* Warnings Tab */}
            <TabsContent value="warnings">
                <DeviceLimitWarning 
                  deviceStats={deviceStats}
                  isLoading={isStatsLoading}
                />
            </TabsContent>
 
 
          </Tabs>
        </CardContent>
      </Card>
    </div>
  );
}